package com.kgc.sso.controller;

import com.kgc.entity.TbUser;
import com.kgc.sso.service.UserService;
import com.kgc.sso.utils.CookieUtils;

import com.kgc.utils.ItripResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping( value = "/user/check/{param}/{type}",method = RequestMethod.GET)
    @ResponseBody
    public ItripResult checkData(@PathVariable("param")String param, @PathVariable("type")Integer type){
        ItripResult itripResult = userService.checkData(param, type);
        return itripResult;
    }

    @RequestMapping ("/user/showLogin")
    public String login(Model model
            , @RequestParam(name= "url",defaultValue ="",required = false)String url){
        model.addAttribute("redirect",url);
        return "login";
    }

    @RequestMapping("/user/showRegister")
    public String register(){
        return "register";
    }
    @RequestMapping("/user/register")
    @ResponseBody
    public ItripResult doregister(TbUser tbUser){
        ItripResult result = userService.register(tbUser);
        return result;
    }

   /*ItripResult result=  userService.userLogin(username,password);
        if(result.getStatus()==200){//登录成功
            //将用户的token写到cookie中
            CookieUtils.setCookie(request,response,"USERTOKENID",result.getData().toString(),1800,true);
        }
        return result;*/
   @RequestMapping("/user/login")
   @ResponseBody
   public ItripResult doLogin(String username, String password,
                              HttpServletRequest request,HttpServletResponse response){
       ItripResult result = userService.userLogin(username,password);
       if (result.getStatus()==200){
           CookieUtils.setCookie(request,response,"USERTOKENID",result.getData().toString(),1800,true);
       }
       return result;
   }
    @RequestMapping("/user/token/{token}")
    @ResponseBody
    public Object getUserByToken(@PathVariable("token")String token,String callback){

        ItripResult result=userService.getUserByToken(token);
        //处理json跨域请求
        if(!StringUtils.isEmpty(callback)){
            //请求参数中有callback，表示一个jsonp的请求(要求json跨域请求)
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
            //设置回调方法
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        //非json跨域请求
        return result;
    }
    @RequestMapping("user/jihuo/{code}")
    @ResponseBody
    private void re(@PathVariable("code")String code){
       userService.jihuo(code);
    };

}
